<!-- 资源绑定 | 作者：menghao.yan | 邮箱：yxs2615@163.com | 时间：2023年08月16日 22:30:38 -->
<template>
  <!-- 弹出框 -->
  <a-modal v-model:open="visible" title="资源绑定" width="500px" class="y-modal" okText="创建" @ok="onSubmit" @cancel="onCancel">
    <a-form :label-col="{ span: 6 }" :wrapper-col="{span: 14}">
      <a-form-item label="所属控制器" v-bind="validateInfos.controller">
        <a-select v-model:value="modelRef.controller" placeholder="请选择所属控制器">
          <a-select-option value="controller-1">controller-1</a-select-option>
          <a-select-option value="controller-2">controller-2</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="物理卷" v-bind="validateInfos.wuLiJuan">
        <a-input v-model:value="modelRef.wuLiJuan" placeholder="请输入DNS服务器" readonly />
      </a-form-item>
      <a-form-item label="虚拟IP" v-bind="validateInfos.xuNiIp">
        <a-input v-model:value="modelRef.xuNiIp" placeholder="请输入虚拟IP" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const { visible } = toRefs(props)

// 定义组件的事件
const emits = defineEmits(['update:visible'])

import { Form } from 'ant-design-vue'

const useForm = Form.useForm

const modelRef = reactive({
  controller: 'controller-1',
  wuLiJuan: 'md10',
  xuNiIp: ''
})

const rulesRef = reactive({
  controller: [
    {
      required: true,
      message: '请选择控制器'
    }
  ],
  wuLiJuan: [
    {
      required: true,
      message: '请输入物理卷'
    }
  ],
  xuNiIp: [
    {
      required: true,
      message: '请输入虚拟IP'
    }
  ]
})

const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, rulesRef)
const onSubmit = () => {
  validate()
    .then(() => {
      console.log('资源绑定>>', toRaw(modelRef))
      onCancel()
    })
    .catch(err => {
      console.log('error', err)
    })
}

const onCancel = () => {
  // 触发自定义事件来通知父组件关闭弹窗
  emits('update:visible', false)
}
</script>

<style scoped>
.ant-row {
  margin-bottom: 10px;
}
</style>